package test;

import java.util.EmptyStackException;

/**
 * @ClassName MyStack
 * @Description: TODO
 * @Author cjc
 * @Date 2020/5/30
 * @Version V1.0
 **/

public class MyStack {
    /**
     * 存数据数组
     */
    private int[] arrays;

    /**
     *最大长度
     */
    private int maxSize;
    /**
     *
     */
    private int top;
   public void MyStack(int size){
        this.arrays = new int[size];
       this. maxSize = size;
       this. top=-1;

    }

    /**
     * 入栈
     * @param value
     */
    public void push(int value){
        if(top<=maxSize-1){
            arrays[top++] = value;
        }
    }

    /**
     * 出栈
     * @return
     */
    public int pop(){
        if(isEmpty()){
            return arrays[top];
        }
          return arrays[top--] ;
    }
    /**
     * 是否为空数组
     * @return
     */
    public boolean isEmpty(){
          return top==-1 ;
    }

    /**
     * 获取栈顶元素
     * @return
     */
    public Object peek(){
        if(top == -1){
            throw new EmptyStackException();
        }
        return arrays[top];
    }
}
